package cn.tedu.carehome.foundation.controller;

import cn.tedu.carehome.foundation.pojo.dto.StaffAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.StaffLoginDTO;
import cn.tedu.carehome.foundation.pojo.vo.StaffListItemVO;
import cn.tedu.carehome.foundation.security.LoginPrincipal;
import cn.tedu.carehome.foundation.service.IStaffService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "员工管理")
@Validated
@RequestMapping("/staff")
public class StaffController {

    public StaffController(){
        log.debug("创建控制类对象:StaffController");
    }
    @Autowired
    private IStaffService staffService;
    @PostMapping("/login")
    @ApiOperation("员工登录")
    @ApiOperationSupport(order = 100)
    public JsonResult<String> login(StaffLoginDTO staffLoginDTO){
      log.debug("开始处理添加员工帐号的请求，参数:{}");
        String jwt = staffService.login(staffLoginDTO);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('staff/add-new')")
    @ApiOperation("添加员工帐号")
    @ApiOperationSupport(order = 200)
    public JsonResult<Void> addNew(@Valid StaffAddNewDTO staffAddNewDTO){
        log.debug("开始处理添加员工帐号的请求，参数：{}",staffAddNewDTO);
        staffService.addNew(staffAddNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation("根据id删除删除员工")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('staff/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据id删除删除员工】的请求，参数：{}", id);
        staffService.deleteById(id);
        return JsonResult.ok();
    }
    @ApiOperation("启用员工")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('staff/update')")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用员工】的请求，参数：{}", id);
        staffService.setEnable(id);
        return JsonResult.ok();
    }


    @ApiOperation("禁用员工")
    @ApiOperationSupport(order = 401)
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('staff/update')")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用员工】的请求，参数：{}", id);
        staffService.setDisable(id);
        return JsonResult.ok();
    }
    @ApiOperation("查询员工列表")
    @ApiOperationSupport(order = 420)
    @PreAuthorize("hasAuthority('staff/read')")
    @GetMapping("")
    public JsonResult<List<StaffListItemVO>> list(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询员工列表】的请求，参数：无");
        log.debug("当事人：{}", loginPrincipal);
        log.debug("当事人的ID：{}", loginPrincipal.getId());
        log.debug("当事人的用户名：{}", loginPrincipal.getUsername());
        List<StaffListItemVO> list = staffService.list();
        return JsonResult.ok(list);
    }

}
